/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.dao;

import org.ipti.br.tag.entity.Address;
import org.ipti.br.tag.entity.Address_;
import org.ipti.br.tag.entity.Neighborhood_;
import org.ipti.br.tag.entity.Person;
import org.ipti.br.tag.entity.Person_;
import org.ipti.br.tag.entity.Street_;
import org.ipti.br.tag.entity.Zipcode_;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 *
 * @author israel
 */
@Stateless
public class AddressFacade extends AbstractFacade<Address> {
    @PersistenceContext(unitName = "TAG2PU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public AddressFacade() {
        super(Address.class);
    }
    
    @Override
    public void create(Address address) {
        getEntityManager().persist(address);
    }
    
    public Long searchExactAddress(Address address) {
        try {
            CriteriaBuilder cb = em.getCriteriaBuilder();
            CriteriaQuery<Long> cq = cb.createQuery(Long.class);
            Root<Address> rt = cq.from(Address.class);
            cq.select(cb.count(rt.get(Address_.id)));
            cq.where(cb.and(
                    cb.equal(rt.join(Address_.zipcode).get(Zipcode_.id), address.getZipcode().getId()),
                    cb.equal(rt.join(Address_.neighborhood).get(Neighborhood_.id), address.getNeighborhood().getId())),
                    cb.equal(rt.join(Address_.street).get(Street_.id), address.getStreet().getId()),
                    cb.like(cb.upper(rt.get(Address_.number)), address.getNumber().toUpperCase()),
                    cb.like(cb.upper(rt.get(Address_.complement)), address.getComplement().toUpperCase())
                    );
            return getEntityManager().createQuery(cq).getSingleResult();
        } catch(Exception e) {
            return null;
        }
    }
    
    public List<Address> modifiedAddressBetweenDate(Calendar initialDate, Calendar finalDate, Long serialNumber) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Address> cq = cb.createQuery(Address.class);
        Root<Address> from = cq.from(Address.class);
        Predicate predicate1 = cb.notEqual(from.get(Address_.serialNumber), serialNumber);
        Predicate predicate2 = cb.greaterThanOrEqualTo(from.get(Address_.lastModified), initialDate);
        Predicate predicate3 = cb.lessThan(from.get(Address_.lastModified), finalDate);
        cq.where(cb.and(predicate1, predicate2, predicate3));

        return getEntityManager().createQuery(cq).getResultList();
    }
    
    public List<Person> getPersons(Address address){
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Person> cq = cb.createQuery(Person.class);
        Root<Person> rt = cq.from(Person.class);
        cq.where(cb.equal(rt.get(Person_.address).get(Address_.id), address.getId()));

        return getEntityManager().createQuery(cq).getResultList();
    }
}
